﻿//http://myweb.tiscali.co.uk/gerryc/camera-icons/index.html

//free icons -  http: //mapicons.nicolasmollet.com/


/* Google maps variables */
var infoWindow = null;
var map = null;
var mgr = null;
var cameraArray = new Array();
/*******************************/


function SpeedCamera(x, y, type, speed, direction, angle) {
    //X,Y,TYPE,SPEED,DIRTYPE,DIRECTION
    //34.4743146, 31.40019727, 5, 0, 2, 200
    this.x = x || null;
    this.y = y || null;
    this.cameraType= type || 0;
    this.speed = speed || 0;
    this.direction = direction || 0;
    this.angle = angle || 0;
    this.marker = null;    
}

/* gets camera icon based on it's type */
SpeedCamera.prototype.getIcon = function () {
    switch (this.cameraType) {
        case 5:
        case 1: return "../images/gatso.bmp";
        case 3: return "../images/redlight.bmp";
    }
}

/* Creates marker's infoWindow content */
SpeedCamera.prototype.getMarkerContent = function () {
    return "X: " + this.x + "<br/>Y: " + this.y +"<br/>Type: " + this.cameraType;
}

/* Create a google marker from speed camera object */
SpeedCamera.prototype.createMarker = function (mgr) {
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(this.x, this.y),
        /*map: map,*/
        draggable: false,
        icon: this.getIcon()
        /*animation: google.maps.Animation.DROP,
        position: parliament*/
    });
    mgr.addMarker(marker,5,17);// marker,minZoom,maxZoom
    this.marker = marker;
    this.marker.infoContent = this.getMarkerContent();
    this.marker.setTitle('title');
    google.maps.event.addListener(marker, 'click', function (event) {
        infoWindow.close();
        infoWindow.setContent(marker.infoContent);
        infoWindow.open(map, marker);
    });
}

/*************************************************************
* UI functions - they will be called from user interface  
**************************************************************/
function setCenter(x, y, zoom) {
    map.setCenter(new google.maps.LatLng(y, x));
    map.setZoom(zoom);
}

/* load poi's as json string 
* parse string to object and create cameras */
function loadPois(str) {
    var tmpObj = eval(str);
    for (var i = 0; i < tmpObj.length; i++) {
        var el = tmpObj[i];
        var cam = new SpeedCamera(el.Location.Y, el.Location.X, el.Type, el.Speed, el.Direction);
        cam.createMarker(mgr);
        cameraArray.push(cam);
    }
    mgr.refresh();
    
}
/****************************************************************/
$(document).ready(function () {
    var myOptions =
    {
        zoom: 8,
        center: new google.maps.LatLng(31.40019727, 34.4743146),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    infoWindow = new google.maps.InfoWindow({ content: "<h3>hello</h3>" });
    var listener = google.maps.event.addListener(map, 'bounds_changed', function () {
        mgr = new MarkerManager(map);
        google.maps.event.addListener(mgr, 'loaded', function () {
            var s = "";
            mgr.refresh();
            window.external.AllDocumentElementsReady(); // send to ui --> all elements ready
        });

        google.maps.event.removeListener(listener);
    });

});

